package com.valuegroup.smarttodo.data.service;

import com.valuegroup.smarttodo.data.enums.FilterEnum;
import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.plugins.pickdialog.DateUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class FilterService extends BaseService {

    /**
     * 查询过滤标签,从数据中实时查询已有的标签
     *
     * @param e
     * @return
     */
    public List<String> findFilterTagForRealTime(FilterEnum e) {
        List<String> result = new ArrayList<>();

        //

        return result;
    }

    /**
     * 查询过滤标签,生成默认标签用于数据初始化
     *
     * @param e
     * @return
     */
    public List<String> findGoalFilterTag(FilterEnum e) {
        List<String> result = new ArrayList<>();
        switch (e) {
            case MONTHLY:
                result.addAll(genTagList(e, "", "月", new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
                break;
            case QUARTER:
                result.addAll(genTagList(e, "", "季度", new Integer[]{1, 2, 3, 4}));
                break;
            case YEAR:
                result.addAll(genTagList(e, "", "年", genYear()));
                break;
            case STATUS:
                result.add(setKeyRule(e, "星标", 10));
                result.add(setKeyRule(e, "已实现", 20));
                result.add(setKeyRule(e, "未实现", 21));
                break;
            case RANGE_GOAL:
                result.add(setKeyRule(e, "月度", AppStatus.GoalsRange.MONTHLY));
                result.add(setKeyRule(e, "季度", AppStatus.GoalsRange.QUARTER));
                result.add(setKeyRule(e, "年度", AppStatus.GoalsRange.YEAR));
                break;
        }
        return result;
    }

    public List<String> findWeekFilterTag(FilterEnum e, int year) {
        List<String> result = new ArrayList<>();
        switch (e) {
            case SORT:
                result.add(setKeyRule(e, "升序", 0));
                result.add(setKeyRule(e, "降序", 1));
                break;
            case RANGE_WEEK:
                result.add(setKeyRule(e, "近三周", 0));
                result.add(setKeyRule(e, "指定周", 1));
                result.add(setKeyRule(e, "指定年", 2));
                break;
            case NEARLY_THREE_WEEKS:
                int nowWeek = DateUtils.getWeekOfYear(new Date());
                Integer[] nearly_three_weeks_Arr = new Integer[]{nowWeek - 1, nowWeek, nowWeek + 1};
                result.addAll(genTagList(e, "", "周", nearly_three_weeks_Arr));
                break;
            case YEAR:
                result.addAll(genTagList(e, "", "年", genYear()));
                break;
            case WEEKS:
                // 取到指定年份的最后一天
                Date lastDay = DateUtils.getYearLast(year);
                // 算出最後一周
                int weekSize = DateUtils.getWeekOfYear(lastDay);
                // 年末的最後一天有可能是第二年的第一周，如果得到的是第一周則日期逐天往前推
                int moveDay = 1;
                while (weekSize == 1) {
                    weekSize = DateUtils.getWeekOfYear(DateUtils.moveDay(lastDay, -moveDay));
                    moveDay++;
                }

                Integer[] weekArr = new Integer[weekSize];
                for (int i = 0; i < weekSize; i++) {
                    weekArr[i] = i + 1;
                }
                result.addAll(genTagList(e, "", "周", weekArr));
                break;
        }
        return result;
    }

    private List<String> genTagList(FilterEnum e, String prefix, String suffix, Integer[] values) {
        List<String> result = new ArrayList<>();
        for (Integer v : values) {
            // 用于TAG展示(前缀+数值+后缀)#用于类型解析(数据类型)_用于类型解析(数值)
            result.add(setKeyRule(e, prefix, suffix, v));
        }
        return result;
    }


    private String setKeyRule(FilterEnum e, String prefix, String suffix, Integer val) {
        return String.format(Locale.CHINESE, "%s%d%s#%d_%d", prefix, val, suffix, e.getPosition(), val);
    }

    private String setKeyRule(FilterEnum e, String key, Integer val) {
        return String.format(Locale.CHINESE, "%s#%d_%d", key, e.getPosition(), val);
    }


    private Integer[] genYear() {
        int YEAR_START = DateUtils.getYear(new Date());
        int yearLimt = 5;
        ArrayList<Integer> tem = new ArrayList<>();

        for (int i = YEAR_START - 1; i < YEAR_START; i++) {
            tem.add(i);
        }
        tem.add(YEAR_START);

        for (int i = YEAR_START + 1; i < YEAR_START + yearLimt; i++) {
            tem.add(i);
        }
        return tem.toArray(new Integer[0]);
    }
}
